package com.liveneo.adapter.webservice.server.claim.service.impl;

import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;

import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.outinterface.util.HttpRequestUtil;
import com.liveneo.adapter.outinterface.util.JsonUtils;
import com.liveneo.adapter.outinterface.util.UtilValidateModel;
import com.liveneo.adapter.webservice.server.claim.model.BaseClaimResponse;
import com.liveneo.adapter.webservice.server.claim.model.CancelInfoDto;
import com.liveneo.adapter.webservice.server.claim.model.EndInfoDto;
import com.liveneo.adapter.webservice.server.claim.model.GetPaperDocDto;
import com.liveneo.adapter.webservice.server.claim.model.ReuploadInfoDto;
import com.liveneo.adapter.webservice.server.claim.model.SendInfoResultResponse;
import com.liveneo.adapter.webservice.server.claim.model.SendSurveyInfoDto;
import com.liveneo.adapter.webservice.server.claim.service.CaseInfoCxfService;


/**
 * 提供核心的接口实现类
 * 
 * @author fucy
 *
 */
@WebService(serviceName = "CaseInfoCxfServiceImpl",
        portName = "CaseInfoCxfServiceImplPort",
        targetNamespace = "http://survey.ws.msplatform.com",
        endpointInterface = "com.liveneo.adapter.webservice.server.claim.service.CaseInfoCxfService")
public class CaseInfoCxfServiceImpl implements CaseInfoCxfService {


	/**
	  * 
	  * @param paramCalendar
	  * @param paramStringHolder1
	  * @param paramStringHolder2
	  
	@Override
	public BaseClaimResponse addLossItem(LossItemSurveyRequest request) {
		System.out.println(request.toString());
		
		
		return  new BaseClaimResponse("1","success");
	}
*/
	/**
	  * 核损完成通知
	  * @param paramString1
	  * @param paramString2
	  * @param paramStringHolder1
	  * @param paramStringHolder2
	  */
	@Override
	public BaseClaimResponse endInfo(EndInfoDto request) {
		BaseClaimResponse bc = new BaseClaimResponse();
		try {
			EndInfoDto endInfo = request;
			if (endInfo == null) {
				throw new RuntimeException("请求参数错误或无效");
			}
			//参数校验
			String valdata = UtilValidateModel.validateModel(endInfo);
			if(StringUtils.isNotBlank(valdata)){
				throw new RuntimeException(valdata);
			}
			LogUtils.info("核损完成通知接收报文：" + JsonUtils.toJson(endInfo));
			bc.setErrMsg("查勘平台：核损完成通知成功");
			bc.setIfSuccess("0");
		} catch (Exception e) {
			LogUtils.error("接口核损完成通知[C02----->endInfo]服务器异常", e);
			String msg = "内部未处理异常";
			if (e instanceof RuntimeException) {
				msg = e.getMessage();
			}
			bc.setErrMsg("查勘平台：核损完成通知" + msg);
			bc.setIfSuccess("0");
		}
		return bc;
	}

	/**
	  * @param paramString1
	  * @param paramString2
	  * @param paramStringHolder1
	  * @param paramStringHolder2
	  
	@Override
	public BaseClaimResponse setCaseNormal(CaseNormalSurveyRequest request) {
		BaseClaimResponse bc = new BaseClaimResponse();
//		try {
//			if (request == null) {
//				throw new AppException("0301", "请求参数错误");
//			}
//			LogUtils.info("案件转人工查勘接收报文：" + request.toString());
//			PrpmCaseInfo caseInfo = DBTableUtil.selectCaseInfoByNo(request.getSetCaseNormal().getCaseNo(), null);
//			if (caseInfo == null) {
//				throw new AppException("0301", "案件信息错误或不存在");
//			}
//			DBTableUtil.updateCaseInfoStatusByCaseNo(Constants.CASE_APPLYSCHE, caseInfo.getUuid());
//			// 推送start
//			String message = "尊敬的客户，此案件已转为人工查勘模式，稍后查勘员会联系您，如有疑问，请拨打【95505】咨询！";
//			// 推送
//			bc.setErrMsg("查勘平台案件转人工查勘：成功");
//			bc.setIfSuccess("1");
//		} catch (Exception e) {
//			String msg = "查勘平台：案件转人工查勘处理异常";
//			if (e instanceof AppException) {
//				msg = e.getMessage();
//			}
//			bc.setErrMsg(msg);
//			bc.setIfSuccess("0");
//		}
		return bc;
	}
*/
	/**
	  * @param paramString1
	  * @param paramString2
	  * @param paramArrayOfLossItemSurveyType
	  * @param paramString3
	  
	@Override
	public BaseClaimResponse sendSurveyInfo(SendSurveyInfoRequest request) {
		
		return  new BaseClaimResponse("0","error");
	}
*/
	/**
	  * 添加查勘员或修理厂
	  * @param paramStringHolder1
	  * @param paramStringHolder2
	  
	@Override
	public BaseClaimResponse addSurvry(AddSurvryRequest request) {
		BaseClaimResponse bc = new BaseClaimResponse();
		try {
//			if(request == null){
//				throw new AppException("0700","请求参数异常");
//			}
//			LogUtils.info("添加查勘员或修理厂："+request.toString());
//			//查询查勘员信息
//			PrpmSurveyUser user = DBTableUtil.selectSurveyUserByCode(request.getSurveyNo());
//			//查询组织机构
//			PrpmOrg prpmOrg = DBTableUtil.selectOrgByCode(request.getUnitCode());
//			//参数检查
//			if(prpmOrg == null){
//				throw new AppException("0710","理赔通知修改查勘员手机号时，没有该查勘员所在机构！");
//			}
//			if(!"1".equals(request.getType())){
//				throw new AppException("0720","理赔通知修改查勘员手机号时，查勘员类型错误！");
//			}
//			String sit = request.getSituation();
//			String name = request.getSurveyName();
//			String mobile = request.getSurveyPhone();
//			String code = request.getSurveyNo();
//			String address = request.getAddress();
//			//
//			if(user == null){
//				String saveSql = "INSERT INTO prpm_survey_user(id,password,surveyUserCode,surveyName,mobile,surveyUserType,validStatus,CreateTime,OrgUuid,address) values(?,?,?,?,?,?,?,?,?,?)";
//				Ps ps = new Ps(UUIDUtils.uuid(),Encryption.encryptMD5ToString(Constants.PASSWORD),code,name,mobile,sit,"1",new Date(),prpmOrg.getId(),address);
//				DB.update(saveSql, ps);
//			}else{
//				String upSql = "UPDATE prpm_survey_user SET surveyName=?,mobile=?,surveyUserType=?,address=?,orgUuid=? where id=?";
//				DB.update(upSql, new Ps(name,mobile,sit,address,prpmOrg.getId(),user.getId()));
//			}
			bc.setErrMsg("成功");
			bc.setIfSuccess("0");
		} catch (Exception e) {
			LogUtils.error("理赔通知修改查勘员手机号异常：",e);
			bc.setErrMsg("理赔通知修改查勘员手机号异常，"+e.getMessage());
			bc.setIfSuccess("0");
		}
		return  bc;
	}*/

	/** 报案注销
	  * @param paramString
	  * @param paramStringHolder1
	  * @param paramStringHolder2
	  */
	@Override
	public BaseClaimResponse cancelCaseInfo(CancelInfoDto cancelInfo) {
		BaseClaimResponse bc = new BaseClaimResponse();
		try {
			
			if (cancelInfo == null) {
				throw new RuntimeException("请求参数错误或无效");
			}
			//参数校验
			String valdata = UtilValidateModel.validateModel(cancelInfo);
			if(StringUtils.isNotBlank(valdata)){
				throw new RuntimeException(valdata);
			}
			LogUtils.info("报案注销通知接收报文：" + JsonUtils.toJson(cancelInfo));
			bc.setErrMsg("查勘平台：报案注销成功");
			bc.setIfSuccess("0");
		} catch (Exception e) {
			LogUtils.error("接口报案注销通知[C07------>cancelInfo]服务器返回异常：\n", e);
			String msg = "内部未处理异常";
			if (e instanceof RuntimeException) {
				msg = e.getMessage();
			}
			bc.setErrMsg("查勘平台：报案注销通知" + msg);
			bc.setIfSuccess("0");
		}
		return bc;
	}

	/**
	  * 单证补传通知接口
	  * @param 
	  * @param 
	  * @param 
	  * @param 
	  */
	@Override
	public BaseClaimResponse test2(ReuploadInfoDto reuploadInfo) {
		BaseClaimResponse bc = new BaseClaimResponse();
		try {
			if (reuploadInfo == null) {
				throw new RuntimeException("请求参数错误或无效");
			}
			//参数校验
			String valdata = UtilValidateModel.validateModel(reuploadInfo);
			if(StringUtils.isNotBlank(valdata)){
				throw new RuntimeException(valdata);
			}
			//String caseNo = reuploadInfo.getCaseNo();
			LogUtils.info("单证补传通知接收报文：" + JsonUtils.toJson(reuploadInfo));
			// 更新
			//String upSql = "update prpm_case_info set ReUploadFlag=? where uuid=?";
			//DB.update(upSql, new Object[] { "1", caseInfo.getCaseInfoId() });

			
			bc.setErrMsg("查勘平台：单证补传通知成功");
			bc.setIfSuccess("0");
		} catch (Exception e) {
			LogUtils.error("接口单证补传通知[C08----->reuploadInfo]服务器异常：\n", e);
			String msg = "未知处理异常";
			if (e instanceof RuntimeException) {
				msg = e.getMessage();
			}
			bc.setErrMsg("查勘平台：单证补传通知" + msg);
			bc.setIfSuccess("0");
		}
		return bc;
	}

	/** 
	  * 补传纸质单证通知
	  * @param rquest
	  */
	@Override
	public BaseClaimResponse test3(GetPaperDocDto getPaperDoc) {
		BaseClaimResponse bc = new BaseClaimResponse();
		try {
			if (getPaperDoc == null) {
				throw new RuntimeException("请求参数错误或无效");
			}
			//参数校验
			String valdata = UtilValidateModel.validateModel(getPaperDoc);
			if(StringUtils.isNotBlank(valdata)){
				throw new RuntimeException(valdata);
			}
			LogUtils.info("补传纸质单证通知：" + JsonUtils.toJson(getPaperDoc));
			String caseNo = getPaperDoc.getCaseNo();
			String docName = getPaperDoc.getDocName();
			// 消息推送
			String message = "";
			if ("0".equals(getPaperDoc.getIsTask())) {
				message = "案件" + caseNo + "的纸质单证已由单证人员" + docName + "成功收集。";
			} else {
				message = "案件" + caseNo + "，请将下列纸质单证提交至单证人员：" + docName;
			}
			String newsType = null;
			if ("0".equals(getPaperDoc.getIsTask())) {
				newsType = "11";// 纸质单证完成
			} else {
				newsType = "9";// 纸质单证补传推送
			}
			// 推送消息
			HttpRequestUtil.sendMessageInfo(caseNo, newsType, getPaperDoc.getSurveyNo(),
					getPaperDoc.getSurveyPhone(), 2, message);
			bc.setErrMsg("查勘平台：补传纸质单证通知成功");
			bc.setIfSuccess("1");
		} catch (Exception e) {
			LogUtils.error("接口补传纸质单证通知[C09----->getPaperDoc]服务器异常：\n", e);
			String msg = "内部未处理异常";
			if (e instanceof RuntimeException) {
				msg = e.getMessage();
			}
			bc.setErrMsg("查勘平台：补传纸质单证通知" + msg);
			bc.setIfSuccess("0");
		}
		return bc;

	}

	/**
	  *  案件转正常案件
	  * @param request
	  * @return
	  * 
	  */
	@Override
	public SendInfoResultResponse test4(SendSurveyInfoDto sendSurveyInfo) {
		SendInfoResultResponse sn = new SendInfoResultResponse();
		try {
			if (sendSurveyInfo == null) {
				throw new RuntimeException("请求参数错误或无效");
			}
			//参数校验
			String valdata = UtilValidateModel.validateModel(sendSurveyInfo);
			if(StringUtils.isNotBlank(valdata)){
				throw new RuntimeException(valdata);
			}
			sn.setErrMsg("查勘平台：案件转正常案件成功");
			sn.setIfSuccess("1");
		} catch (Exception e) {
			String msg = "查勘平台：案件转正常案件处理异常";
			if (e instanceof RuntimeException) {
				msg = e.getMessage();
			}
			sn.setErrMsg(msg);
			sn.setIfSuccess("0");
			sn.setIsPay("0");
		}
		return sn;
	}

	
}
